package com.sdap2.controller;

import com.sdap2.common.model.ApiResult;
import com.sdap2.dto.UserLoginDTO;
import com.sdap2.dto.UserRegisterDTO;
import com.sdap2.entity.SysUser;
import com.sdap2.service.SysUserService;
import com.sdap2.vo.UserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户管理")
@RestController
@RequestMapping("/api/auth")
@RequiredArgsConstructor
public class SysUserController {
    
    private final SysUserService sysUserService;
    
    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public ApiResult<String> login(@Valid @RequestBody UserLoginDTO loginDTO) {
        String token = sysUserService.login(loginDTO);
        return ApiResult.success(token);
    }
    
    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public ApiResult<Void> register(@Valid @RequestBody UserRegisterDTO registerDTO) {
        sysUserService.register(registerDTO);
        return ApiResult.success();
    }
    
    @Operation(summary = "获取当前用户信息")
    @GetMapping("/current")
    public ApiResult<UserVO> getCurrentUser(@RequestHeader("Authorization") String token) {
        SysUser user = sysUserService.getCurrentUser(token);
        return ApiResult.success(UserVO.fromEntity(user));
    }
} 